package com.zky.org.service.impl;

import com.zky.basic.service.impl.BaseServiceImpl;
import com.zky.util.PageList;
import com.zky.org.domain.Department;
import com.zky.org.mapper.DepartmentMapper;
import com.zky.org.query.DepartmentQuery;
import com.zky.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 业务实现类：部门
 */
@Service

public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {

@Autowired
private DepartmentMapper departmentMapper;

    @Override
    public List<Department> getDeptTree() {
        List<Department> departments = departmentMapper.findAll();
        Map<Long, Department> map = new HashMap<>();
        for (Department department : departments) {
            map.put(department.getId(),department);
        }
        List<Department> deptTree = new ArrayList<>();
        for (Department department : departments) {
            if (department.getParent_id()==null){
                deptTree.add(department);
            }else {
                Department department1 = map.get(department.getParent_id());
                department1.getChildren().add(department);
            }

        }

        return deptTree;
    }


}
